package data
{
	import equipments.PB;
	import equipments.PU;
	
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.utils.Timer;
	import flash.utils.describeType;
	
	import flashx.textLayout.formats.FormatValue;
	import flashx.textLayout.formats.LineBreak;
	import flashx.textLayout.formats.TextAlign;
	
	import gs.TweenLite;
	
	import mx.collections.ArrayList;
	import mx.controls.Button;
	import mx.controls.VScrollBar;
	import mx.core.Application;
	import mx.core.Container;
	import mx.core.FlexSprite;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.events.FlexEvent;
	import mx.graphics.SolidColor;
	import mx.graphics.SolidColorStroke;
	import mx.graphics.Stroke;
	import mx.managers.DragManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	import mx.skins.Border;
	import mx.validators.EmailValidator;
	
	import net.data.Economic;
	
	import spark.components.BorderContainer;
	import spark.components.DataGrid;
	import spark.components.Group;
	import spark.components.Image;
	import spark.components.Label;
	import spark.components.VScrollBar;
	import spark.effects.animation.Animation;
	import spark.layouts.TileLayout;
	import spark.layouts.supportClasses.LayoutBase;
	
	import views.ChangeIconWin;
	import views.ChangeNameWin;
	
	public class MyBorderContainer extends BorderContainer
	{
//		[Embed(source = 'img/branch/base.gif')]
//		private var BaseImg:Class;
		
		private var _image:Image;
		private var _label:Label;
		
		public var pu_num:uint=1;	//pu의 갯수저장 
		
		private var _imgUrl:Object;
		
		private var _title:String;
		
		private var _iName:String;
		
		//tag id
		private var _tagId:String;
		
		//초기투자비
		private var _initInvest:String
		//운영비
		private var _operExpr:String
		//교체연수
		private var _yearExca:String
		
		private var _detailViewCon:BorderContainer=new BorderContainer();
		private var _scrBar:spark.components.VScrollBar=new spark.components.VScrollBar();
		private var _detailView:Group=new Group();
		
		//EQ의 Scenario를 위한 변수들
		private var _detailViewConForScenario:BorderContainer=new BorderContainer();
		private var _scrBarForScenario:spark.components.VScrollBar=new spark.components.VScrollBar();
		private var _detailViewForScenario:Group=new Group();
		
		//detailView의 각 라인의 높이
		private var detailViewLineHeightArr:Array=[];
		//detailView의 각 라인의 Y값
		private var detailViewLineYArr:Array=[];
		
		private var _categori:String;
//		public var btTimer:Timer

		public var detailBt:Button;
		private var changeBt:Button;
		private var deleteBt:Button;
//		private var connectBt:Button;
		
		private var _item:XML
		
		private var _idx:uint
		
		private var _upLeveBc:MyBorderContainer
		
		private var _detailArr:ArrayList=new ArrayList();
		private var _detailArrForScenario:ArrayList=new ArrayList();
		
		
		
		
		public var isHit:Boolean=false;

		public var gridArr:ArrayList= new ArrayList();
		
		public var seGrid:SEgrid;
		public var ceGrid:CEgrid;
		public var eqGrid:EQgrid;
		
		
		
		public var gridVar:Number=110;
			//gridArr()메서드로 grid를 추가할때..
			//pu의 갯수에 따라 gird의 높이를 지정한 변수를 올려준다
		public var gridHeight:Number=0;
			//gridArr() 메서드 실행시 계속해서 누적으로
			//높이값을 쌓아놓는 변수
		
		private var connetArr:ArrayList=new ArrayList();
		
		//this 와 연결된 앞에 bc
		public var pBc:Vector.<MyBorderContainer>;
		
		//this 와 연결된 뒤에 bc
		public var nBc:Vector.<MyBorderContainer>;
		
		
		public var targetConnectPointNum:uint
		
		
		//라인긋기를 위한 변수들
		private var _lineLayerArr:Vector.<LineLayer>

		private var area:BorderContainer;

		private var menuItem1:ContextMenuItem;

		private var menuItem2:ContextMenuItem;

		private var menuItem3:ContextMenuItem;

		private var menuItem4:ContextMenuItem;
		
		private var menuItem5:ContextMenuItem;
		
		private var menuItem6:ContextMenuItem;

		private var _itemId:String
		
		
		//나에게서 화살표가 시작되어 연결되있는 bc들의 uid를 담는 Array
		 public var outBcUidArr:Array;
		 
		 private var ro:RemoteObject;
		 
		 public var econo:net.data.Economic;
		 
		
// public methos ========================================================================================
		public function MyBorderContainer()
		{
			trace("== MyBorderContainer Generate!! ==");
			super();
			
			var idDate:Date=new Date();
			var idDateStr:String = idDate.getFullYear()+intToString(idDate.getMonth()+1,2)+intToString(idDate.getDate(),2)+intToString(idDate.getHours(),2)+intToString(idDate.getMinutes(),2)+intToString(idDate.getSeconds(),2)+intToString(idDate.getMilliseconds(),3);
			this.itemId = idDateStr;

			this.imgUrl = 'ram/Admin/System_Img/Upload/base.gif';
			
			//nBc, pBc 초기화
			nBc=new Vector.<MyBorderContainer>();
			pBc=new Vector.<MyBorderContainer>();
			
			this.backgroundFill=new SolidColor(0,0);
//			contextMenuSet();
			
			this.setStyle("borderAlpha",0);
			
//			_detailViewCon.width=931;
//			_detailViewCon.height=430;
			_detailViewCon.x=310;
			_detailViewCon.y=159;
			
			_detailView.id="detailView";
			_detailView.width=931;
			_detailView.height=428;
			_detailView.x = 0;
			_detailView.y = 0;
			
			_detailViewCon.addElement(_detailView);
			_detailViewCon.addElement(scrBar);
			
			scrBar.viewport=_detailView;
			scrBar.stepSize=20;
			scrBar.x=915;
			scrBar.y=0;
			scrBar.height=429;
			scrBar.value=_detailView.height;
			scrBar.minimum=0;
			scrBar.maximum=100;
			scrBar.addEventListener(Event.CHANGE,scrChghdl);
			
			
			_lineLayerArr=new Vector.<LineLayer>();
			
			//마우스에 반응할 area 설정
			area = new BorderContainer();
			area.backgroundFill=new SolidColor(0,0);
			area.setStyle("borderAlpha",0);
			area.width=70;
			area.height=70;
			addElement(area);
			
			ro = new RemoteObject("remoteRO");
			ro.addEventListener(ResultEvent.RESULT,roEc_resulthandler);
			
			
		}
		
		
		private function roEc_resulthandler(e:ResultEvent):void{
		
			var eco:net.data.Economic = net.data.Economic(e.result);  
				
			Maros.home.economicAnalysis.changeYear.text =String(eco.CI); 
			Maros.home.economicAnalysis.operatingExpenses.text = String(eco.CE);
			Maros.home.economicAnalysis.investmentCost.text = String(eco.durableYears);
		
		}

		public function get yearExca():String
		{
			return _yearExca;
		}

		public function set yearExca(value:String):void
		{
			_yearExca = value;
		}

		public function get operExpr():String
		{
			return _operExpr;
		}

		public function set operExpr(value:String):void
		{
			_operExpr = value;
		}

		public function get initInvest():String
		{
			return _initInvest;
		}

		public function set initInvest(value:String):void
		{
			_initInvest = value;
		}
		
		//ScenarioView를 위해 _detailViewConForScenario에 _deatilView와 scrBarForScenario를 셋팅함
		public function get detailViewConForScenario():BorderContainer
		{
			return _detailViewConForScenario;
		}

		public function set detailViewConForScenario(value:BorderContainer):void
		{
			_detailViewConForScenario = value;
		}

		public function get detailViewForScenario():Group
		{
			return _detailViewForScenario;
		}

		public function set detailViewForScenario(value:Group):void
		{
			_detailViewForScenario = value;
		}

		public function get detailArrForScenario():ArrayList
		{
			return _detailArrForScenario;
		}

		public function set detailArrForScenario(value:ArrayList):void
		{
			_detailArrForScenario = value;
		}

		private function makeScenarioView():void{
			_detailViewConForScenario.x=310;
			_detailViewConForScenario.y=159;
			
			
			_detailViewForScenario.id="detailView";
			_detailViewForScenario.width=931;
			_detailViewForScenario.height=428;
			_detailViewForScenario.x = 0;
			_detailViewForScenario.y = 0;
			_detailViewConForScenario.backgroundFill=new SolidColor(0xeeffff,1.0);
			
			_detailViewConForScenario.addElement(_detailViewForScenario);
			_detailViewConForScenario.addElement(_scrBarForScenario);
			
			//"ScenarioView" 라벨 추가
			var label:Label=new Label();
			label.text = "Scenario View";
			label.y=-11;
			label.setStyle("fontSize","15");
			label.setStyle("fontWeight","bold");
			_detailViewConForScenario.addElement(label);
			
			_scrBarForScenario.viewport=_detailViewForScenario;
			_scrBarForScenario.stepSize=20;
			_scrBarForScenario.x=915;
			_scrBarForScenario.y=0;
			_scrBarForScenario.height=429;
			_scrBarForScenario.value=_detailViewForScenario.height;
			_scrBarForScenario.minimum=0;
			_scrBarForScenario.maximum=100;
			_scrBarForScenario.addEventListener(Event.CHANGE,scrChghdl);
			
			
		}
		
		//context menu에서 scenario를 선택했을때 실행되는 함수
		private function viewScenarioView():void{
			Maros.selectedItemArr.push(this);
			Maros.home.changeItem("sc"); // sc = scenario
			Maros.home.addElement(_detailViewConForScenario);
			_detailViewForScenario.addEventListener(DragEvent.DRAG_ENTER,Maros.home.dragEnterHandler);
			_detailViewForScenario.addEventListener(DragEvent.DRAG_DROP,Maros.home.dropHandler);
			
			Maros.home.tree.selectedItem = Object(item); 
			
			//			upLeveBc = Maros.home.curItem as MyBorderContainer;    
			trace("upLeveBc:",upLeveBc);
			
			Maros.home.curItem = MyBorderContainer(this);  
			/*
			트리에서 현재 아이템이 접혀있으면 펼치기.
			*/
			Maros.home.openParents(Maros.home.curItem.item);
			
			//시나리오 모드임을 알린다.
			Maros.home._isScenarioMode=true;
			//자신과 똑같은 EQ를 만들어서 _detailViewForScenario에 먼저 하나를 붙여놓는다.
			if(detailArrForScenario.length==0){
				Maros.home.item_bt_setting("eq",0,true,"scenario","scenario");
			}
		}
		
		
		[Bindable("tagIdChanged")]
		public function get tagId():String
		{
			return _tagId;
		}

		public function set tagId(value:String):void
		{
			_tagId = value;
			this.dispatchEvent(new Event("tagIdChanged"));
		}

		public function resetScrBar(_detailViewHeight:Number):void{
			detailViewCon.removeElement(scrBar);
			scrBar = new spark.components.VScrollBar();
			_detailViewCon.addElement(scrBar);
			trace("detailView.height :",detailView.height);
			trace("_detailViewHeight :",_detailViewHeight);
			scrBar.viewport=_detailView;
			trace("viewport.contentHeight :",scrBar.viewport.contentHeight);
			scrBar.stepSize=20;
			scrBar.x=915;
			scrBar.y=0;
			scrBar.height=429;
			scrBar.value=_detailViewHeight;
			scrBar.minimum=0;
			scrBar.maximum=100;
			
			//스크롤바의 위치를 맞추기 위해.. 더미 container를 하나 추가함
			var dmCon:BorderContainer = new BorderContainer();
			dmCon.y = _detailViewHeight;
			dmCon.height=1;
			dmCon.width=1;
			_detailView.addElement(dmCon);
//			_detailView.removeElement(dmCon);
		}
		
		
		public function get scrBar():spark.components.VScrollBar
		{
			return _scrBar;
		}
		
		public function set scrBar(value:spark.components.VScrollBar):void
		{
			_scrBar = value;
		}

		public function get itemId():String
		{
			return _itemId;
		}

		public function set itemId(value:String):void
		{
			_itemId = value;
		}

		public function addData():void{
			//데이터 그리드에 데이터를 넣는다
			if(_iName=="se"){
				seGrid.dataAdd();
				scrBar.visible=false;
			}else if(_iName=="ce"){
				ceGrid.dataAdd();
				scrBar.visible=false;
			}else if(_iName=="eq"){
				eqGrid.dataAdd(this); 
				scrBar.visible=false;
			}
		}
		
		
		
		
		
		/*
			현재 MyBorderContainer의 BR을 구하기.
				현재 아이템이 BR일때는 상관없지만..
				BR이 아닐때는 구할 필요가 있다.
		*/
		public function getBranch():MyBorderContainer{
			var temp:MyBorderContainer=this;
			while(temp.upLeveBc!=null){
				temp=temp.upLeveBc;
			}
			return temp;
		}
		
		
		
		
		
		
		
		/*
			_puNum :  pu의 갯수
			_xml : pb의 xml item (tree 구성시 추가할 xml)
		*/
		public function addPB(_puNum:uint,_xml:XML,_iName:String):PB{
			trace("================= MyBorderContainer.addPB("+_puNum, _xml, _iName+") =================");
			
			
			var len:uint = this.detailArr.length;
			var pb:PB=new PB();
			pb.upLeveBc = Maros.home.curItem;
			
			pb.title=_iName;
			pb.item=_xml;
			pb.iName = _iName;
			
			gridAdd((65*_puNum)+50);
			var lastGrid:BorderContainer=BorderContainer(gridArr.getItemAt(gridArr.length-1));
			pb.grid=lastGrid;
			pb.x = lastGrid.x+10;
			pb.y = lastGrid.y+10;
			detailView.addElement(pb);
			var _item:XML = new XML("<node label='"+pb.title+"' ename='"+pb.iName+"' bc='"+pb+"'/>");
			XML(Maros.home.curItem.item).appendChild(_item);
			pb.item = _item;
			
			
			pb.puSetting(_puNum);
			
			//gridHeight를 pu의 갯수에 따라 셋팅
			gridVar = pb.height+40;
				
			var arrIndex:uint = Math.floor(detailArr.length/10)
				
			if(detailViewLineHeightArr[arrIndex]<gridVar||detailViewLineHeightArr[arrIndex]==null){
				detailViewLineHeightArr[arrIndex]=gridVar
			}
			if(arrIndex==0){
				detailViewLineYArr[arrIndex]=10;
			}else{
				if(detailViewLineYArr[arrIndex]<detailViewLineYArr[arrIndex-1]+detailViewLineHeightArr[arrIndex-1]||detailViewLineYArr[arrIndex]==null){
					detailViewLineYArr[arrIndex]=detailViewLineYArr[arrIndex-1]+detailViewLineHeightArr[arrIndex-1];
				}
			}
			pb.idx = detailArr.length
			_detailArr.addItem(pb);
			
			return pb;
		}
		
		public function pu_detailArrPush(_i:MyBorderContainer):void{
			_i.x = 20;
			_i.y = _detailArr.length*90+20;
			_detailArr.addItem(_i);
		}
		public function pu_reAlign():void{
			trace("pu_reAlign",this._detailArr.length);
			var pu:MyBorderContainer
			for(var i:uint=0;i<this._detailArr.length;++i){
				pu=_detailArr.getItemAt(i) as MyBorderContainer;
				pu.x=20;
				pu.y=(i*90)+20;
			}
		}
		
		/*
		eq나 se등을 모델링할때..가로 순서대로 늘어나는 continer일때 사용하는 메서드 
		*/
		public function detailArrPush(_i:MyBorderContainer):void{
			
			var arrIndex:uint = Math.floor(detailArr.length/10);
			
			//pb를 제외한 다른 아이템들은 110을 높이값으로 한다.
			// pb를 추가할 경우에는 addPB메서드에서 높이값을 셋팅한다.
			if(detailViewLineHeightArr[arrIndex]<110||detailViewLineHeightArr[arrIndex]==null){
				detailViewLineHeightArr[arrIndex] = 110;
			}
			trace("line height:",detailViewLineHeightArr);
			
			if(arrIndex==0){
				detailViewLineYArr[arrIndex]=10;
			}else{
				if(detailViewLineYArr[arrIndex]<detailViewLineYArr[arrIndex-1]+detailViewLineHeightArr[arrIndex-1]||detailViewLineYArr[arrIndex]==null){
					detailViewLineYArr[arrIndex]=detailViewLineYArr[arrIndex-1]+detailViewLineHeightArr[arrIndex-1];
				}
			}
			gridAdd();
			
			_detailArr.addItem(_i);
		}
		
		/*
		Scenario용 container 메서드
		*/
		public function detailArrPushForScenario(_i:MyBorderContainer):void{
			
//			var arrIndex:uint = Math.floor(detailArr.length/10);
			
//			//pb를 제외한 다른 아이템들은 110을 높이값으로 한다.
//			// pb를 추가할 경우에는 addPB메서드에서 높이값을 셋팅한다.
//			if(detailViewLineHeightArr[arrIndex]<110||detailViewLineHeightArr[arrIndex]==null){
//				detailViewLineHeightArr[arrIndex] = 110;
//			}
//			trace("line height:",detailViewLineHeightArr);
//			
//			if(arrIndex==0){
//				detailViewLineYArr[arrIndex]=10;
//			}else{
//				if(detailViewLineYArr[arrIndex]<detailViewLineYArr[arrIndex-1]+detailViewLineHeightArr[arrIndex-1]||detailViewLineYArr[arrIndex]==null){
//					detailViewLineYArr[arrIndex]=detailViewLineYArr[arrIndex-1]+detailViewLineHeightArr[arrIndex-1];
//				}
//			}
			
			gridAddForScenario();
			_detailArrForScenario.addItem(_i);
		}
		
		
		public function set detailArr(value:ArrayList):void
		{
			_detailArr = value;
		}
		
		public function gridRemove(_idx:uint):void{
			
			//			trace("MyBorderContainer.gridRemove(_idx)");
			//			trace("_idx:",_idx);
			//			trace("gridArr:",gridArr);
			var grid:BorderContainer=gridArr.getItemAt(_idx) as BorderContainer;
			trace("grid:",grid);
			_detailView.removeElement(grid);
			gridArr.removeItemAt(_idx);
			
			reAlignGrid();
			reAlignItems();
		}
		
		public function dragEnter_hdl(e:DragEvent):void{
			//어레이를 바꿈
			trace("=== dragEnter_hdl ===")
			var FORMAT:String="box";
			var item:MyBorderContainer = e.dragInitiator as MyBorderContainer;
			var itemIdx:int;
			if(Maros.home._isScenarioMode){
				itemIdx= detailArrForScenario.getItemIndex(item);
			}else{
				itemIdx= detailArr.getItemIndex(item);
			}
			var startGrid:BorderContainer = gridArr.getItemAt(itemIdx) as BorderContainer;
			if( e.dragSource.hasFormat(FORMAT) )
			{
				
				DragManager.acceptDragDrop( BorderContainer( e.currentTarget ) );
				
				//드래그할려는 자리의 인덱스
				var posIdx:uint = parseInt(BorderContainer(e.currentTarget).name.substr(4))
				
				detailArr.removeItem(item);
				detailArr.addItemAt(item,posIdx);
				
				gridArr.removeItem(startGrid);
				gridArr.addItemAt(startGrid,posIdx);
				reAlignGrid();
				reAlignItems();
			}
		}
		public function dragDrop_hdl(e:DragEvent):void{
			trace("dragDrop_hdl");
			//TODO::드래그 한 아이템을 detailArr에서 빼고,, drop한 index에 다시 넣기
			//			var item:MyBorderContainer = e.dragInitiator as MyBorderContainer;
			//			var idx:int=-1
			//			for(var i:uint=0;i<_detailView.numElements;++i){
			//				if(_detailView.getElementAt(i)===item){
			//					idx=i;
			//					break;
			//				}
			//			}
			//			trace("idx:",idx);
			//			if(idx==-1){
			//				//아이템 복사
			//				
			//			}
			//			
			//			reAlignItems();
		}
		
		public function createLineLayer():LineLayer{
			var ll:LineLayer=new LineLayer(this);
			_lineLayerArr.push(ll);
			addElementAt(ll,0);
			ll.deleteBt_setting();
			return ll;
		}
		
		
		//라인위에 마우스 오버시 핸들러
		private function lineDelete_hdl(e:MouseEvent):void{
			
		}
		public function deleteLineLayer(ll:LineLayer):void{
			trace("삭제하기전:",_lineLayerArr.length);
			for(var i:uint=0;i<_lineLayerArr.length;++i){
				if(_lineLayerArr[i]==ll){
					_lineLayerArr.splice(i,1);
				}
			}
			trace("삭제한후:",_lineLayerArr.length);
			
		}
		
//		public function timer_hdl(event:Event=null):void
//		{
//			//			btTimer.stop()
//			try{
//				this.removeElement(detailBt)
//				this.removeElement(changeBt)
//				this.removeElement(deleteBt)
//				//				if(iName=="br"){
//				//					this.removeElement(connectBt);
//				//				}
//			}catch(e:Error){}
//		}
		
		//this의 pBc의 lineLayer 중에 this와 연결된 lineLayer를 구한다 
		public function getLineLayer_InPBC():Vector.<LineLayer>{
			var rtn:Vector.<LineLayer>=new Vector.<LineLayer>;
			var p:MyBorderContainer; 
			var ll:LineLayer;
			for(var i:uint=0;i<pBc.length;++i){
				p = pBc[i];
				for(var j:uint=0;j<p.lineLayerArr.length;++j){
					ll=p.lineLayerArr[j];
					if(ll.finishBC===this){
						rtn.push(ll);
					}
				}
			}
			return rtn;
		}
		
		//this의 nBc의 lineLayer 중에 this와 연결된 lineLayer를 구한다 
		public function getLineLayer_InNBC():Vector.<LineLayer>{
			var rtn:Vector.<LineLayer>=new Vector.<LineLayer>;			 
			var ll:LineLayer;
			
			for(var j:uint=0;j<lineLayerArr.length;++j){
				ll=lineLayerArr[j];				
				rtn.push(ll);
				
			}
			
			return rtn;
		}
		
		//더블클릭시 '상세보기'로 이동'
		public function setEventDoubleClick():void{
			this.doubleClickEnabled=true;
			this.addEventListener(MouseEvent.DOUBLE_CLICK,detailBt_hdl);
		}
		
		public function detailBt_hdl(e:MouseEvent):void
		{
			trace("detailBt_hdl");
			/*
				pb를 더블클릭했을때는 selectedItemArr에 추가하지 않는다.
				pb는 구조상으로는 PU의 상위이지만.. 모델링상에서는 pu 와 같은 단계의 depth를 가진다.
			*/
			ExternalInterface.call("console.log","e.currentTarget.iName:"+e.currentTarget.iName); 
			if(e.currentTarget.iName=="pb"){
			
			}else{
				// pb를 제외한 나머지
				Maros.selectedItemArr.push(this);
			}
			Maros.home.changeItem(iName);
			if(Maros.home._isScenarioMode){
				Maros.home.addElement(_detailViewConForScenario);
				_detailViewForScenario.addEventListener(DragEvent.DRAG_ENTER,Maros.home.dragEnterHandler);
				_detailViewForScenario.addEventListener(DragEvent.DRAG_DROP,Maros.home.dropHandler);
				Maros.home.curItem = MyBorderContainer(this);  
			}else{
				Maros.home.addElement(_detailViewCon);
				_detailView.addEventListener(DragEvent.DRAG_ENTER,Maros.home.dragEnterHandler);
				_detailView.addEventListener(DragEvent.DRAG_DROP,Maros.home.dropHandler);
				Maros.home.tree.selectedItem = Object(item); 
				Maros.home.curItem = MyBorderContainer(this);  
	//				트리에서 현재 아이템이 접혀있으면 펼치기.
				Maros.home.openParents(Maros.home.curItem.item);
			}
		}
		
		//detailView의 재정렬을 위한 메서드
		//pb에서 pu가 추가될 경우 재정렬을 해야 함
		public function detailViewAlign():void{
			// 1. 줄마다 확인
			// 2. 한줄안에 PB가 있는지 확인
			// 3. pb가 있으면 높이값을 구해 선을 다시 연결
			
			//각 라인의 높이값을 저장하는 array
			var lineHeightArr:Array=[];
			var lineTotalNum:uint = Math.ceil(detailArr.length/10);
			
			//detailArr에서 pb를 찾아서 인덱스값을 가지고 있는다.
			for(var j:uint=0;j<lineTotalNum;++j){
				lineHeightArr.push(searchPB(j));
			}
			
			//lineHeightArr를 가지고 아이템들을 재배열한다
			for(var k:uint=0;k<detailArr.length;++k){
				
			}
				
				
			//한줄안에서 pb를 찾는다
			function searchPB(lineNum:uint):Number{
				//한줄의 높이값
				var lineHeight:Number=90;
				var item:MyBorderContainer
				for(var i:uint = (lineNum*10);i<((lineNum*10)+10);++i){
					if(i>=detailArr.length){
						break;
					}
					item = MyBorderContainer(detailArr.getItemAt(i));
					if(item.iName=="pb"){
						lineHeight = item.height;
					}
				}
				return lineHeight;
			}
			
		}

		
		
		
// private method ========================================================================================
		private function contextMenuSet():void
		{
			var cm:ContextMenu = new ContextMenu();
			
			//기본메뉴 삭제
			//			this.contextMenu.hideBuiltInItems();
			cm.hideBuiltInItems();
			
			//사용자 메뉴 추가
			menuItem1=new ContextMenuItem("link");
			menuItem2=new ContextMenuItem("rename");
			menuItem3=new ContextMenuItem("remove");
			menuItem4=new ContextMenuItem("icon change");
			menuItem5=new ContextMenuItem("Economic");
			menuItem6=new ContextMenuItem("Scenario");
			
			
			ExternalInterface.call("console.log","this.iName : "+this.iName);
			//PB에는 라인긋기 메뉴가 필요없다.  
			if(this.iName == "br"){
				cm.customItems.push(menuItem1);
				menuItem1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextBt_hdl);
			}
			
			
			// EQ일때는 economic analisys add
			// iName은 내가 br을 더블클릭하여 안으로 들어왔을때 더블클릭한 아이템을 나타낸다
			// iname은 순서대로 null>br>eq 순이다.
			if(this.iName == "eq"){
				
				cm.customItems.push(menuItem5);
				menuItem5.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextBt_hdl);
				
				if(!Maros.home._isScenarioMode){
					cm.customItems.push(menuItem6);
					menuItem6.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextBt_hdl);
				}
			}
			
			cm.customItems.push(menuItem2);
			menuItem2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextBt_hdl);
			
			cm.customItems.push(menuItem3);
			menuItem3.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextBt_hdl);
			
			cm.customItems.push(menuItem4);
			menuItem4.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextBt_hdl);
			
			this.contextMenu = cm;
			
		}
		
		private function contextBt_hdl(e:ContextMenuEvent):void
		{
			if(e.currentTarget.caption=="link"){
				Maros.home.connectLine(this);
				return;
			}
			if(e.currentTarget.caption=="rename"){
				changeName();
				return;
				
			}
			if(e.currentTarget.caption=="remove"){
				deleteBt_hdl();
				return;
			}
			if(e.currentTarget.caption=="icon change"){
				changeBt_hdl(this);
				return;
			}
			if(e.currentTarget.caption=="Economic"){
				//econo = new net.data.Economic();
				ro.getEconomic(this.itemId);
				openEconomicAnalysisWin();
				
				return;
			}
			if(e.currentTarget.caption=="Scenario"){
				viewScenarioView();
				return;
			}
		}
		
		private function openEconomicAnalysisWin():void
		{
			Maros.home.changingItem=this;
			Maros.home.wall.visible=true;
			Maros.home.setElementIndex(Maros.home.wall,Maros.home.numElements-1);
			Maros.home.setElementIndex(Maros.home.economicAnalysis,Maros.home.numElements-1);
			
			Maros.home.economicAnalysis.visible=true;
			
		}
		
		public function changeName():void
		{
			Maros.home.changingItem=this;
			Maros.home.wall.visible=true;
			Maros.home.setElementIndex(Maros.home.wall,Maros.home.numElements-1);
			Maros.home.setElementIndex(Maros.home.changeNameWin,Maros.home.numElements-1);
			Maros.home.changeNameWin.visible=true;
			
			if(this.iName=="eq"){
				Maros.home.changeNameWin.currentState = "eq";
				Maros.home.changeNameWin.changeNameIT.text = this.title;
				Maros.home.changeNameWin.changeTagIdIT.text = this.tagId;
				Maros.home.changeNameWin.changeNameIT.setFocus();
			}else{
				Maros.home.changeNameWin.currentState = "basic";
				Maros.home.changeNameWin.changeNameIT.text = this.title;
				Maros.home.changeNameWin.changeNameIT.setFocus();
				
			}
		}		
		
		
		private function deleteBt_hdl():void
		{
			trace("IDX:",idx);
			var _iidx:uint
			if(this.upLeveBc==null){	//br일경우
				_iidx = Maros.home.branchArr.getItemIndex(this);
				Maros.home.branchArr.removeItemAt(_iidx);
				delete XML(Maros.home.treeData.node).node[_iidx];
				Maros.home.area.removeElement(this);
				var i:uint=0, j:uint=0,k:uint;
				var p:MyBorderContainer;
				var ll:LineLayer;				
				
				if(pBc.length!=0){
					for(i=0;i<pBc.length;++i){
						p=pBc[i];
						
						for(j=0;j<p.lineLayerArr.length;++j){
							ll = p.lineLayerArr[j]; 
//							trace("ll",ll);
//							trace("ll",ll.finishBC);
							if(ll.finishBC===this){//LineLayer 삭제
								trace("LineLayer를 찾아서 삭제했음");
								p.removeElement(ll);
								trace("p.lineLayerArr.length:",p.lineLayerArr.length);
								p.lineLayerArr.splice(j,1);
								trace("p.lineLayerArr.length:",p.lineLayerArr.length);
								break;
							}
						}
						
						//p의 nBc에서 this를 삭제
						for(j=0;j<p.nBc.length;++j){
							if(p.nBc[j]===this){		
								trace("p.nBc에서 this를 찾아서 삭제했음");
								p.nBc.splice(j,1);
								break;
							}
						}
						
						Maros.home.linkedArr.removeItem(p)
					}
				}
				
				if(nBc.length!=0){
					Maros.home.linkedArr.removeItem(this);
					//nBc에서 this와 연결된 내용 삭제
					for(i=0;i<nBc.length;++i){
						p=nBc[i];
						for(j=0;j<p.pBc.length;++j){
							if(p.pBc[j]===this){
								p.pBc.splice(j,1);
							}
						}
					}
				}
			}else{ //bc가 아닐 경우
				
				_iidx = this.upLeveBc.detailArr.getItemIndex(this);
				this.upLeveBc.detailArr.removeItemAt(_iidx);
				delete XML(this.upLeveBc.item).node[_iidx];
				this.upLeveBc.detailView.removeElement(this);
				if(iName=="pu"){
					this.upLeveBc.detailArr.removeItem(this);
					this.upLeveBc.pu_reAlign();
					this.upLeveBc.pu_num-=1;
					var temp:Array = this.upLeveBc.label.text.split("\nblock : ");
					this.upLeveBc.label.text=temp[0]+"\nblock : "+this.upLeveBc.pu_num;
					
				}else{
					this.upLeveBc.gridRemove(_iidx);
					
				}
			}
		}
		
		
		private function changeBt_hdl(item:MyBorderContainer):void
		{
			
			Maros.home.changingItem=item;
			Maros.home.wall.visible=true;
			Maros.home.setElementIndex(Maros.home.wall,Maros.home.numElements-1);
			Maros.home.setElementIndex(Maros.home.changeIconWin,Maros.home.numElements-1);
			Maros.home.changeIconWin.visible=true;
		}
		
//		private function bt_hdl(e:MouseEvent):void{
//			if(e.type==MouseEvent.MOUSE_OVER){
//				//				btTimer.stop()
//				this.addElement(changeBt);
//				this.addElement(detailBt);
//				this.addElement(deleteBt);
//				//				if(iName=="br"){
//				//					this.addElement(connectBt);
//				//				}
//				
//				return;
//			}
//			if(e.type==MouseEvent.MOUSE_OUT){
//				//				btTimer.start();
//				return;
//			}
//		}
		
		
		
		
		
		//그리드 하나를 추가하고 선 긋기 
		private function gridAdd(_h:Number=90):void{
			
			//그리드 기본적으로 1개 깔아놓기-다음 그리드는 아이템 하나가 추가되면 하나씩 더 추가함
			var gridBc:BorderContainer=new BorderContainer();
			gridBc.width=90;
			gridBc.height=_h;
			var len:uint = detailArr.length;
			trace("detailArr Len:",len);
			if(len%10==0&&len!=0){
				gridBc.x=10;
				gridBc.y=detailViewLineYArr[Math.floor(len/10)-1]+detailViewLineHeightArr[Math.floor(len/10)-1];
				trace("gridBc.y:",gridBc.y);
			}else{
				//				trace("CASE B");
				gridBc.x=(len%10)*90+10;
				gridBc.y=(len==0)?10:detailViewLineYArr[Math.floor(len/10)];
			}
			
			trace("gridHeight:",gridVar);
			
			if(len!=0){
				if(len%10==0){
					gridBc.graphics.lineStyle(1,0x000000);
					gridBc.graphics.moveTo(10,45);
					gridBc.graphics.lineTo(0,45);
					gridBc.graphics.lineTo(0,-10);
					gridBc.graphics.lineTo(858,-10);
					
					//마지막 그리드와의 높이값을 구해서 선을 잇는다
					var lastGrid:BorderContainer=BorderContainer(gridArr.getItemAt(gridArr.length-1));
					gridBc.graphics.lineTo(858,-(gridBc.y-(lastGrid.y+lastGrid.height)));
					gridVar=110;
				}else{
					gridBc.graphics.lineStyle(1,0x000000);
					gridBc.graphics.moveTo(20,45);
					gridBc.graphics.lineTo(-20,45);
				}
			}
			
			gridBc.backgroundFill=new SolidColor(0xff0000,0);
			gridBc.borderStroke=new Stroke(0,1,0);
			_detailView.addElementAt(gridBc,0);
			trace("_detailView.height:",_detailView.height);
			gridBc.name = "grid"+gridArr.length;
			gridArr.addItem(gridBc);
			gridBc.addEventListener(DragEvent.DRAG_ENTER, dragEnter_hdl);
			//gridBc.addEventListener(DragEvent.DRAG_DROP, dragDrop_hdl);
		}
		
		//scenario용 gridAdd
		private function gridAddForScenario():void{
			
			//그리드 기본적으로 1개 깔아놓기-다음 그리드는 아이템 하나가 추가되면 하나씩 더 추가함
			var gridBc:BorderContainer=new BorderContainer();
			gridBc.width=90;
			gridBc.height=90;
			var len:uint = _detailArrForScenario.length;
			gridBc.x=10;
			gridBc.y=len*90+10
			
			if(gridArr.length>0){
				gridBc.graphics.lineStyle(1,0x000000);
				gridBc.graphics.moveTo(45,10);
				gridBc.graphics.lineTo(45,-10);
			}
			
			gridBc.backgroundFill=new SolidColor(0xff0000,0);
			gridBc.borderStroke=new Stroke(0,1,0);
			_detailViewForScenario.addElementAt(gridBc,0);
			gridBc.name = "grid"+gridArr.length;
			gridArr.addItem(gridBc);
			gridBc.addEventListener(DragEvent.DRAG_ENTER, dragEnter_hdl);
			//gridBc.addEventListener(DragEvent.DRAG_DROP, dragDrop_hdl);
		}
		
		//그리드만 정렬
		public function reAlignGrid():void{
			
			
			//detailView에 있는 detailVewLineHeightArr 와 detailViewLineYArr를 새로 구한다.
			var lineTotalNum:uint = Math.ceil(gridArr.length/10);
			for(var j:uint=0;j<lineTotalNum;++j){
				//각 라인별로 최대값 구하기
				detailViewLineHeightArr[j] = getMaxHeightFromLine(j);
				detailViewLineYArr[j] = (j==0)?10:detailViewLineYArr[j-1]+detailViewLineHeightArr[j-1];
			}
			trace("detailViewLineHeightArr:",detailViewLineHeightArr);
			trace("detailViewLineYArr:",detailViewLineYArr);
			
			
			var grid:BorderContainer;
			var len:uint
			for(var i:uint=0;i<gridArr.length;++i){
				grid = gridArr.getItemAt(i) as BorderContainer;
				grid.name="grid"+i;
				grid.graphics.clear();
				var arrIndex:uint = Math.floor(i/10);
				if(i%10==0){
					grid.x=10;
					
					grid.y=detailViewLineYArr[arrIndex];
					
					grid.graphics.clear();
					grid.graphics.lineStyle(1,0x000000);
					
					//					grid.graphics.moveTo(5,40);
					//					grid.graphics.lineTo(10,45);
					//					grid.graphics.lineTo(5,50);
					
					
					grid.graphics.moveTo(10,45);
					grid.graphics.lineTo(0,45);
					grid.graphics.lineTo(0,-10);
					grid.graphics.lineTo(858,-10);
					//마지막 그리드와의 높이값을 구해서 선을 잇는다
					if(i!=0){
						var lastGrid:BorderContainer=BorderContainer(gridArr.getItemAt(i-1));
						grid.graphics.lineTo(858,-(grid.y-(lastGrid.y+lastGrid.height)));
					}
				}else{
					grid.x=(i%10)*90+10;
					grid.y=detailViewLineYArr[arrIndex];
					grid.graphics.clear();
					grid.graphics.lineStyle(1,0x000000);
					grid.graphics.moveTo(10,45);
					grid.graphics.lineTo(-20,45);
				}
			}
			
			//맨처음의 grid에는 선없애기
			
			if(gridArr.length>0){
				var fGrid:BorderContainer=gridArr.getItemAt(0) as BorderContainer;
				fGrid.graphics.clear();
			}
			
			//라인별 최대 높이값 구하기
			function getMaxHeightFromLine(_lineNum:uint):Number{
				var mxH:Number = 110
				var grid:BorderContainer;
				for(var i:uint = _lineNum*10;i<(_lineNum*10)+10;++i){
					if(i>=(gridArr.length))break;
					grid = gridArr.getItemAt(i) as BorderContainer;
					//					trace("==",grid.height,"==");
					if((grid.height+20)>mxH){
						//						trace("== 최대값발견 ==:",grid.height);
						mxH=grid.height+20;
					}
				}
				return mxH;
			}
		}
		
		
		//detailArr에 순서대로 아이템 정렬
		public function reAlignItems():void{
			trace("reAlignItems")
			var grid:BorderContainer;
			var item:MyBorderContainer;
			
			var posX:Number,posY:Number;
			
			trace("detailArr.length:",detailArr.length);
			for(var i:uint=0;i<detailArr.length;++i){
				grid = gridArr.getItemAt(i) as BorderContainer;
				
				item = detailArr.getItemAt(i) as MyBorderContainer;
				
				posX = grid.x+10;
				posY=grid.y+10;
				
				TweenLite.to(item,0.2,{x:posX,y:posY})
			}
		}
		
		protected function scrChghdl(event:Event):void
		{
			trace(scrBar.value);
			//			if(_detailView.height<=scrBar.height){
			//				scrBar.enabled=false;
			//				return;
			//			}else{
			//				scrBar.enabled=true;
			//					
			//			}
			
			//			_detailView.y = (_detailView.height-431)/100*-scrBar.value;
			trace("_detailView.height:",_detailView.height);
			trace("_detailView.y:",_detailView.y);
		}
		//int를 ?자리 숫자형 string 으로 반환
		private function intToString(
			_n:uint, //string으로 변환할 int값
			_p:uint	//변환할 글자 갯수
		):String{
			var n:String = _n.toString();
			
			//추가할 pre글자수
			var preNum:uint = _p - n.length;
			
			var rtnStr:String="";
			for(var i:uint=0;i<preNum;++i){
				rtnStr+="0";
			}
			rtnStr+=n;
			
			return rtnStr;
		}
		
		

		
// getter / setter =======================================================================================		
		public function get lineLayerArr():Vector.<LineLayer>
		{
			return _lineLayerArr;
		}

		public function set lineLayerArr(value:Vector.<LineLayer>):void
		{
			_lineLayerArr = value;
		}

		
		
		
		public function get detailViewCon():BorderContainer
		{
			return _detailViewCon;
		}

		public function set detailViewCon(value:BorderContainer):void
		{
			_detailViewCon = value;
		}

		public function get image():Image
		{
			return _image;
		}

		public function set image(value:Image):void
		{
			_image = value;
		}

		public function get label():Label
		{
			return _label;
		}

		public function set label(value:Label):void
		{
			_label = value;
		}

		public function get categori():String
		{
			return _categori;
		}

		public function set categori(value:String):void
		{
//			if(value!="branch"){
//				this.contextMenu.customItems.shift();
//			}
			_categori = value;
		}

		public function get imgUrl():Object
		{
			return _imgUrl;
		}
		
		[Bindable]
		public function set imgUrl(value:Object):void
		{
			_imgUrl = value;
			if(!image){
				image=new Image();
				addElement(image);
				image.width=70;
				image.y = 5;
			}
			image.source=imgUrl;
//			image.horizontalAlign="center";
		}
		
		[Bindable("titleChanged")]
		public function get title():String
		{
			return _title;
		}

		public function set title(value:String):void
		{
			_title = value;
			if(!label){
				label=new Label();
				addElement(label);
				label.width=70;
				label.y = 50;
				label.maxDisplayedLines=1;
				label.showTruncationTip=true;
			}
			
			
			label.text = title;
			
//			label.x = (this.width-label.width)/2;
			this.dispatchEvent(new Event("titleChanged"));
		}

		public function get detailView():Group
		{
			return _detailView;
		}

		public function set detailView(value:Group):void
		{
			_detailView = value;
		}
		
		

		public function get iName():String
		{
			return _iName;
		}

		public function set iName(value:String):void
		{
			_iName = value;
			//마지막 단계에 가서는 엑셀시트처럼 그리드로 보여준다
			if(_iName=="se"){
				seGrid=new SEgrid()
				this._detailView.addElement(seGrid);
				seGrid.parentMBC = this;
			}else if(_iName=="ce"){
				ceGrid=new CEgrid()
				this._detailView.addElement(ceGrid);
			}else if(_iName=="eq"){
				eqGrid=new EQgrid()
				if(Maros.home._isScenarioMode){
					eqGrid.setStyle("contentBackgroundColor","0xeeffff");
					this._detailViewForScenario.addElement(eqGrid);
				}else{
					this._detailView.addElement(eqGrid);
				}
				eqGrid.parentMBC = this;
				contextMenuSet();
				makeScenarioView();
			}
			contextMenuSet();
			
			
		}

		public function get item():XML
		{
			return _item;
		}

		public function set item(value:XML):void
		{
			_item = value;
		}

		public function get idx():uint
		{
			return _idx;
		}

		public function set idx(value:uint):void
		{
			_idx = value;
		}

		public function get upLeveBc():MyBorderContainer
		{
			return _upLeveBc;
		}

		public function set upLeveBc(value:MyBorderContainer):void
		{
			_upLeveBc = value;
		}

		public function get detailArr():ArrayList
		{
			return _detailArr;
		}
		
		
	}
	
}